<?php

namespace common\modules\openWeixin\components;

use common\modules\openWeixin\models\OpenWechatRegisterMiniprogram;
use common\modules\openWeixin\services\OpenWechatAuthService;
use EasyWeChat\Kernel\BaseClient;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use EasyWeChat\OpenPlatform\Application;
use GuzzleHttp\Exception\GuzzleException;
use Yii;
use yii\base\BaseObject;

class WxappClient extends BaseObject
{
    private Application  $openPlatform;

    public function init(): void
    {
        parent::init(); // TODO: Change the autogenerated stub
        $this->openPlatform = Yii::$app->wechat->openPlatform;
    }

    /**
     * 快速注册企业小程序.
     *
     * @param $name //企业名（需与工商部门登记信息一致）；如果是“无主体名称个体工商户”则填“个体户+法人姓名”，例如“个体户张三”
     * @param $code //企业代码
     * @param $code_type //企业代码类型 1：统一社会信用代码（18 位） 2：组织机构代码（9 位 xxxxxxxx-x） 3：营业执照注册号(15 位)
     * @param $legal_persona_wechat //法人微信号
     * @param $legal_persona_name //法人姓名（绑定银行卡）
     * @param string $component_phone //第三方联系电话
     * @return array
     *
     * @throws InvalidConfigException
     * @throws GuzzleException
     */
    public function registerMiniprogram($name, $code, $code_type, $legal_persona_wechat, $legal_persona_name, string $component_phone = ''): array
    {
        $params = [
            'name'=> $name,
            'code'=> $code,
            'code_type'=> $code_type,
            'legal_persona_wechat'=> $legal_persona_wechat,
            'legal_persona_name'=> $legal_persona_name,
            'component_phone'=> $component_phone
        ];
        return $this->openPlatform->httpPostJson('cgi-bin/component/fastregisterweapp', $params, ['action' => 'create']);
    }

    /**
     * 复用公众号主体快速注册小程序
     * @return array
     *
     * @throws InvalidConfigException
     * @throws GuzzleException
     */
    public function RegisterMiniprogramByOffiaccount()
    {
        $access_token = $this->openPlatform->access_token;
        $ticket = $this->openPlatform['verify_ticket']->getTicket();
        $params = [
            'access_token'=> $access_token,
            'ticket'=> $ticket
        ];
        return $this->openPlatform->httpPostJson('cgi-bin/account/fastregister', $params);
    }

    /**
     * 注册试用小程序
     * @param $name
     * @param $openid
     * @return array
     */
    public function registerBetaMiniprogram($name,$openid)
    {
        $params = [
            'name'=> $name,
            'openid'=> $openid
        ];
        return $this->openPlatform->httpPostJson('/wxa/component/fastregisterbetaweapp', $params);
    }



    public function getAuthorizerList($offset,$count)
    {
        $params = [
            'component_appid'=>Yii::$app->params['wechatOpenPlatformConfig']['app_id'],
            'offset'=> $offset,
            'count'=> $count
        ];
        return $this->openPlatform->httpPostJson('cgi-bin/component/api_get_authorizer_list', $params);

    }
}